<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8"%>
<%@ include file="../include/laypublic.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <title>调度任务管理</title>
</head>
<body>
  <div class="layui-fluid">   
    <div class="layui-card">
      <!-- 查询区域  -->
      <div id="query-form" class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">Trigger名称</label>
            <div class="layui-input-inline">
              <input type="text" name="triggerName" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
            	<infopub:select entireName="-Trigger状态" id="query-form-triggerState" name="triggerState" type="dic" groupNo="A00001028">
				</infopub:select>
            </div>
          </div>
          <shiro:hasPermission name="CODEGEN_QUERY">
	          <div class="layui-inline">
	            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-job-console-search">
	              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
	            </button>
	            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-reset">
	              <i class="layui-icon layui-icon-fonts-clear  layuiadmin-button-btn"></i>重置
	            </button>
	          </div>
          </shiro:hasPermission>
        </div>
      </div>
  	  <!-- 按钮区域  -->
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <shiro:hasPermission name="QRTZ_JOB_ADD">
          	<button class="layui-btn layuiadmin-btn-admin " data-type="add">
          	 <i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>新增
          	</button>
          </shiro:hasPermission>
        </div>
        <!-- 数据表单 -->
        <table id="LAY-job-console-manage" lay-filter="LAY-job-console-manage"></table>
        
        <div class="layui-form" lay-filter="add_from" id="add_from" style="padding: 20px 30px 0 0;display:none">
		    <div class="layui-form-item">
		      <label class="layui-form-label">Job类型</label>
		      <div class="layui-input-inline">
		        <select lay-filter="job_type" name="jobType" lay-verify="required">
 					<option value="0">Cron_Job</option>
 					<option value="1">OneTime_Job</option>
 				</select>
		      </div>
		    </div>
		    <div class="layui-form-item">
		      <label class="layui-form-label">JobBean</label>
		      <div class="layui-input-inline">
		        <input type="text" id="add_jobBeanName" name="jobBeanName" maxlength="40" lay-verify="required" placeholder="请输入JobBean" autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-form-item">
		    	<label class="layui-form-label">Job名称</label>
			    <div class="layui-input-inline">
			      <input type="text" id="add_jobName" name="jobName" maxlength="100" lay-verify="required" placeholder="请输入Job名称" autocomplete="off" class="layui-input">
			    </div>
		  	</div>
		    <div class="layui-form-item">
		    	<label class="layui-form-label">Trigger名称</label>
			    <div class="layui-input-inline">
			      <input type="text" id="add_triggerName" name="triggerName" maxlength="100" lay-verify="required" placeholder="请输入Trigger名称" autocomplete="off" class="layui-input">
			    </div>
		  	</div>
		    <div class="layui-form-item" id="cron_div">
		      <label class="layui-form-label">Cron表达式</label>
		      <div class="layui-input-inline">
		        <input type="text" id="add_cronExp" name="cronExp" maxlength="100" lay-verify="required" placeholder="请输入Cron表达式" autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-form-item" id="one_div" style="display:none">
		      <label class="layui-form-label">执行时间</label>
		      <div class="layui-input-inline">
		        <input type="text" id="add_fireTime" name="fireTime" placeholder="请输入执行时间" autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="add_from_submit" id="add_from_submit" value="确认">
		    </div>
	  	</div>
	  	
	  	<!-- 编辑trigger -->
        <div class="layui-form" lay-filter="edit_from" id="edit_from" style="padding: 20px 30px 0 0;display:none">
        	<input type="hidden" name="triggerName" />
   			<input type="hidden" name="jobName" />
   			<input type="hidden" name="jobType" id="edit_jobType" />
		    <div class="layui-form-item" id="edit_cron_div">
		      <label class="layui-form-label">Cron表达式</label>
		      <div class="layui-input-inline">
		        <input type="text" id="edit_cronExp" name="cronExp" maxlength="100" lay-verify="required" placeholder="请输入Cron表达式" autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-form-item" id="edit_one_div" style="display:none">
		      <label class="layui-form-label">执行时间</label>
		      <div class="layui-input-inline">
		        <input type="text" id="edit_fireTime" name="fireTime" maxlength="100" lay-verify="required" placeholder="请输入执行时间" autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-form-item layui-hide">
		      <input type="button" lay-submit lay-filter="edit_from_submit" id="edit_from_submit" value="确认">
		    </div>
	  	</div>
      </div>
    </div>
  </div>

  <script>
	//json 
	var stateJSON = <infopub:dicJson groupNo="A00001028" />;
  	layui.config({
    	base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
  	}).extend({
    	index: 'lib/index' //主入口模块
  	}).use(['index', 'table', 'laydate'], function(){
    	var $ = layui.$
    	,laydate = layui.laydate
    	,form = layui.form
    	,table = layui.table;
    	
    	laydate.render({
    	      elem: '#add_fireTime'
    	      ,type: 'datetime'
    	    });
    	laydate.render({
    	      elem: '#edit_fireTime'
    	      ,type: 'datetime'
    	    });
    	// 表单
    	table.render({
      		elem: '#LAY-job-console-manage'
      		,url: basePath + '/jobConsole/queryDataByCondition' //请求数据接口
      		,cols: [[
        		{type: 'checkbox', fixed: 'left'}
        		,{field: 'triggerName', title: 'Trigger名称',width: 200}
        		/* ,{field: 'jobName', title: 'Job名称',width: 155} */
        		,{field: 'cronExpression', title: 'Cron表达式', width: 135,  templet: function(d){
    		  			return d.cronExpression||global_null_tip;
    				}
        		}
        		,{field: 'prevFireTime', title: '上次执行时间',width: 170, templet: function(d){
        				return (d.prevFireTime == null||d.prevFireTime == -1) ? global_null_tip : layui.util.toDateString(d.prevFireTime,"yyyy-MM-dd HH:mm:ss");
        			}
        		}
        		,{field: 'nextFireTime', title: '下次执行时间',width: 170,  templet: function(d){
        				return (d.nextFireTime == null||d.nextFireTime == -1) ? global_null_tip : layui.util.toDateString(d.nextFireTime,"yyyy-MM-dd HH:mm:ss");
        			}
        		}
        		,{field: 'triggerState', title: 'Trigger状态', width: 110, templet: function(d){
        	 			return stateJSON[d.triggerState];
	       			}
	    		}
        		,{field: 'triggerType', title: 'Trigger类型', width: 110 }
        		,{field: 'startTime', title: 'Job开始时间',width: 170, templet: function(d){
        				return (d.startTime == null||d.startTime == -1) ? global_null_tip : layui.util.toDateString(d.startTime,"yyyy-MM-dd HH:mm:ss");
        			}
        		}
        		,{title: '操作', width: 280, align:'center', fixed: 'right', templet: function(d){
			      	  	var optStr ="";
			      	  	<shiro:hasPermission name="QRTZ_JOB_UPDATE">
				      		optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
			  	  	  	</shiro:hasPermission>
			      	  	<shiro:hasPermission name="QRTZ_JOB_PAUSE">
				      		optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="pause"><i class="layui-icon layui-icon-edit"></i>暂停</a>';
			  	  	  	</shiro:hasPermission>
			      	  	<shiro:hasPermission name="QRTZ_JOB_RESUME">
				      		optStr += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="resume"><i class="layui-icon layui-icon-edit"></i>启用</a>';
			  	  	  	</shiro:hasPermission>
			  	  	  	<shiro:hasPermission name="QRTZ_JOB_DEL">
			  	    		 optStr += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';
			  		  	</shiro:hasPermission>
			  		  	return optStr;
        			}
        		}
      		]]
      		,page: true
      		,limit: 10
    	});
    
    <shiro:hasPermission name="QRTZ_JOB_QUERY">
	    //监听搜索
	    form.on('submit(LAY-job-console-search)', function(data){
	      //执行重载
	      table.reload('LAY-job-console-manage', {page: {curr: 1},
	        where: trimData(data)
	      });
	    });
	  	//重置清空
	    form.on('submit(LAY-user-back-reset)', function(data){
	    	reset_form('query-form');
	    	//额外处理select
			$("#query-form-triggerState").val('');
	    });
    </shiro:hasPermission>
    
    // 监听操作列
    table.on('tool(LAY-job-console-manage)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){//删除
          layer.confirm('确定删除？',{icon:3, title :'提示'}, function(index){
          	$.ajax({
  				type : 'post',
  				url : basePath + '/jobConsole/delete?triggerName='+data.triggerName+"&jobName="+data.jobName,
  				success:function(result){
  					if(result.success){
  						table.reload('LAY-job-console-manage'); //数据刷新
  						layer.close(index);
  					}else{
  						layer.msg(result.msg,{icon: 7});
  				  	}
  				}
  			});
          });
      }else if(obj.event === 'pause'){//暂停
    	  if(data.triggerState == 'PAUSED'){
    		  layer.alert('该job已暂停！',{icon: 0});
    		  return;
  		  }
          layer.confirm('确定暂停？',{icon:3, title :'提示'}, function(index){
            	$.ajax({
    				type : 'post',
    				url : basePath + '/jobConsole/pause?triggerName='+data.triggerName+"&jobName="+data.jobName,
    				success:function(result){
    					if(result.success){
    						table.reload('LAY-job-console-manage'); //数据刷新
    						layer.close(index);
    					}else{
    						layer.msg(result.msg,{icon: 7});
    				  	}
    				}
    			});
            });
        }else if(obj.event === 'resume'){//启用
        	if(data.triggerState != 'PAUSED'){
        		layer.alert('该job正在运行中！',{icon: 0});
    			return;
    		}
            layer.confirm('确定启用？',{icon:3, title :'提示'}, function(index){
              	$.ajax({
      				type : 'post',
      				url : basePath + '/jobConsole/resume?triggerName='+data.triggerName+"&jobName="+data.jobName,
      				success:function(result){
      					if(result.success){
      						table.reload('LAY-job-console-manage'); //数据刷新
      						layer.close(index);
      					}else{
      						layer.msg(result.msg,{icon: 7});
      				  	}
      				}
      			});
              });
          } else if(obj.event === 'edit'){
        	  if(data.triggerType=='CRON'){
 				 $('#edit_cronExp').attr("lay-verify","required");  
 				 $('#edit_cron_div').show();
 				 $('#edit_fireTime').removeAttr("lay-verify");  
 				 $('#edit_one_div').hide();
 				 $('#edit_jobType').val("0");
 			 }else if(data.triggerType=='SIMPLE'){
 				 $('#edit_cronExp').removeAttr("lay-verify");  
 				 $('#edit_cron_div').hide();
 				 $('#edit_fireTime').attr("lay-verify","required"); 
 				 $('#edit_one_div').show();
 				 $('#edit_jobType').val("1");
 			 }
        	 form.val('edit_from',data); // 填充原值
        	 layer.open({
        		  type: 1
        		  ,title: '编辑Cron'
        		  ,content: $('#edit_from')
        		  ,area: layui.admin.screen() < 2 ? ['80%', '180px'] : ['420px','180px']
        		  ,btn: ['确定', '取消']
        		  ,btnAlign: 'c'
        		  ,yes: function(index, layero){
        			  var submitID = 'edit_from_submit'
        			  ,submit = layero.contents().find('#'+ submitID);
        			  //监听提交
        			  form.on('submit('+ submitID +')', function(data){
        				  var field = data.field; //获取提交的字段
        				  //提交 Ajax 成功后，静态更新表格中的数据
        				  $.ajax({
        					  type : 'post',
        					  url : basePath + '/jobConsole/update',
        					  data: field,
        					  success:function(result){
        						  if(result.success){
        							  table.reload('LAY-job-console-manage'); //数据刷新
        							  layer.close(index); //关闭弹层
        							  clean_form('edit_from');//清空表单
        						  }else{
        							  layer.msg(result.msg,{icon: 7});
        						  }
        					  }
        				  });
        			});  
              submit.trigger('click');
            }
          });
        }
    });
    
    // 按钮事件
    var active = {
      add: function(){
          layer.open({
        	  type: 1
        	  ,title: '添加Cron_Trigger'
        	  ,content: $('#add_from')
        	  ,area: layui.admin.screen() < 2 ? ['80%', '400px'] : ['420px','400px']
        	  ,btn: ['确定', '取消']
        	  ,btnAlign: 'c'
        	  ,yes: function(index, layero){
        		  var submitID = 'add_from_submit'
            		,submit = layero.contents().find('#'+ submitID);
        		  //监听提交
        		  form.on('submit('+ submitID +')', function(data){
        			  var field = data.field; //获取提交的字段
        			  //提交 Ajax 成功后，静态更新表格中的数据
        			  $.ajax({
        				  type : 'post',
        				  url : basePath + '/jobConsole/add',
        				  data: field,
        				  success:function(result){
        					  if(result.success){
        						  table.reload('LAY-job-console-manage'); //数据刷新
        						  layer.close(index); //关闭弹层
        						  clean_form("add_from");//清空表单
        					  }else{
        						  layer.msg(result.msg,{icon: 7});
        					  }
        				  }
              		 });
            	});  
            submit.trigger('click');
          }
        }); 
      }
    }  
    // data-type事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
    
  	$("#add_jobBeanName").change(function () {
		   var jobBeanName =  $("#add_jobBeanName").val();
		   $("#add_jobName").val(jobBeanName+"Name");
		   $("#add_triggerName").val(jobBeanName+"Trigger");
    });
  	
  	form.on('select(job_type)', function(data){
  		var job_type = data.value;
  		if(job_type == 0){
			$('#add_cronExp').attr("lay-verify","required");  
			$('#cron_div').show();
			$('#add_fireTime').removeAttr("lay-verify");  
			$('#one_div').hide();
		}else{
			$('#add_cronExp').removeAttr("lay-verify");  
			$('#cron_div').hide();
			$('#add_fireTime').attr("lay-verify","required");
			$('#one_div').show();
		}
  	});
  });
  	
  </script>
</body>
</html>

